package com.sos.ui
{
	import com.greensock.easing.*;
	import com.greensock.TimelineMax;
	import com.sos.core.cards.CardManager;
	import com.sos.core.game.GameAction;
	import com.sos.core.game.GameActionCard;
	import com.sos.core.game.GameCast;
	import com.sos.core.game.GameCastStatus;
	import com.sos.core.game.GameLogRow;
	import com.sos.core.game.GameProc;
	import com.sos.core.game.GameRole;
	import com.sos.core.game.GameTurn;
	import com.sos.core.LogRowType;
	import com.sos.core.Position;
	import com.sos.core.Stats;
	import com.sos.logging.Logger;
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	
	public class RoundViewer extends MovieClip
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------		
		
		public static const		USER_PLAYER_COLOR:String			= "00CC00";
		public static const		OPPONENT_PLAYER_COLOR:String		= "FF6600";
		protected static const	DEBUG_COLOR:String					= "FFFF33";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _turn:GameTurn;
		private var _playArea:PlayArea;
		private var _timer:Timer;
		private var _delay:Number = 5000;
		private var _itemCounter:Number = 0;
		private var _logCounter:uint = 0;
		private var _currentSection:String;
		private var _currentPlayerId:uint;
		private var _currentProcIndex:uint;
		private var _finished:Boolean = false;
		private var _roundLogIndex:uint;
		private var _roundTexts:Dictionary;
		private var _running:Boolean = false;
		private var _skip:Boolean = false;
		private var _ac:ActionCardClip;
		private var _homeAcList:Vector.<ActionCardClip> = new Vector.<ActionCardClip>;
		private var _awayAcList:Vector.<ActionCardClip> = new Vector.<ActionCardClip>;
		
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function RoundViewer(turn:GameTurn, playArea:PlayArea)
		{
			// Init object
			_turn 				= turn;
			_playArea 			= playArea;
			_currentSection 	= 'init';
			_roundTexts 		= new Dictionary();
			//_playArea.solveOverlay.solveText.htmlText = "";
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get turn():GameTurn
		{
			return _turn;
		}
		
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		private function timeUpHandler(e:TimerEvent):void {
			_timer.stop();
			Logger.debug("UI > Round Viewer > time's up, " + _timer.delay.toString() + " seconds passed");
			if (_logCounter > 0)
				clearCurrentLogItem();
			showNextLogItem();
			if (_finished) 
				stopView();
			else {
				if (_skip)
					_timer.delay = 0;
				_timer.start();
			}
		}
		
		private function showNextLogItem():void {
			var msg:String;
			var attPlayerClip:PlayerClip;
			var defPlayerClip:PlayerClip;
			
			var attPlHtmlText:String;
			var defPlHtmlText:String;
			
			var attackWins:Boolean;
			var defenseWins:Boolean;
			var logRow:GameLogRow = _turn.logRows[_logCounter];
			
			
			switch (logRow.type) {
				case LogRowType.DEBUG:
					//_playArea.solveOverlay.solveText.htmlText = "<font color='#" + DEBUG_COLOR + "'><b>DEBUG:</b> " + logRow.text + "</font><br>" + _playArea.solveOverlay.solveText.htmlText;
					_playArea.solveOverlay.addLogRow("<font color='#" + DEBUG_COLOR + "'><b>DEBUG:</b> " + logRow.text + "</font><br>");
					break;
				case LogRowType.DISCARD:
					if (logRow.targetUser == _playArea.engine.game.player.id) 
						_playArea.solveOverlay.addLogRow("<font color='#" + USER_PLAYER_COLOR + "'><b>You</b></font> " + logRow.text + "<br>");
						//_playArea.solveOverlay.solveText.htmlText = "<font color='#" + USER_PLAYER_COLOR + "'><b>You</b></font> " + logRow.text + "<br>" + _playArea.solveOverlay.solveText.htmlText;
					else 
						_playArea.solveOverlay.addLogRow("<font color='#" + OPPONENT_PLAYER_COLOR + "'><b>Opponent</b></font> " + logRow.text + "<br>");
						//_playArea.solveOverlay.solveText.htmlText = "<font color='#" + OPPONENT_PLAYER_COLOR + "'><b>Opponent</b></font> " + logRow.text + "<br>" + _playArea.solveOverlay.solveText.htmlText;
					break;
				case LogRowType.INIT:
					msg = getActionMessage(_turn.action, _turn.target);
					_playArea.playMenu.setTargetingMessage(msg);
					_playArea.clearActionArrow();
					_playArea.showActionArrow(_turn.action.name, _playArea.fieldMc.playerList[_turn.attackerCardId.toString()], _playArea.fieldMc.playerList[_turn.targetCardId.toString()]);
					break;
				case LogRowType.MAIN:
					attPlayerClip = _playArea.fieldMc.playerList[_turn.solve.attackerId.toString()];
					defPlayerClip = _playArea.fieldMc.playerList[_turn.solve.defenderId.toString()];
					
					attPlayerClip.lockUpForSolvePhase();
					defPlayerClip.lockUpForSolvePhase();
					
					if (_turn.solve.autoWinner == GameRole.IDLE) {
						attPlayerClip.rollDie(_turn.solve.attackerRoll);
						defPlayerClip.rollDie(_turn.solve.defenderRoll);
					}
					else {
					}
					
					break;
				case LogRowType.END_MAIN:
					attPlayerClip = _playArea.fieldMc.playerList[_turn.solve.attackerId.toString()];
					defPlayerClip = _playArea.fieldMc.playerList[_turn.solve.defenderId.toString()];
					
					attPlayerClip.lockUpForSolvePhase();
					defPlayerClip.lockUpForSolvePhase();
					attPlayerClip.hideDie();
					defPlayerClip.hideDie();
					attPlHtmlText = getPlayerNameHtml(attPlayerClip);
					defPlHtmlText = getPlayerNameHtml(defPlayerClip);
					var showActionAnimation:Boolean = false;
					
					if (_turn.solve.autoWinner == GameRole.IDLE) {
						if (_turn.solve.winner == GameRole.ATTACK) {
							msg = attPlHtmlText + " succeeds!<br>";
							showActionAnimation = true;
							
							attackWins = true;
							defenseWins = false;
						}
						else {
							msg = defPlHtmlText + " recovers the ball!<br>";
							attackWins = false;
							defenseWins = true;
						}
						msg = defPlHtmlText + " rolled " + _turn.solve.defenderRoll + ", his score is " + _turn.solve.defenderScore + "<br>" + msg;
						msg = attPlHtmlText + " rolled " + _turn.solve.attackerRoll + ", his score is " + _turn.solve.attackerScore + "<br>" + msg;
						
						
						
						
					}
					else {
						if (_turn.solve.autoWinner == GameRole.ATTACK) {
							showActionAnimation = true;
							msg = attPlHtmlText + " succeeds (auto)!<br>";
							attackWins = true;
							defenseWins = false;
						}
						else {
							msg = defPlHtmlText + " succeeds (auto)!<br>";
							attackWins = false;
							defenseWins = true;
						}
					}
					//SHOW SCORE AND WIN/LOSE TEXT ON PLAYERS
					attPlayerClip.showScore(_turn.solve.attackerScore, attackWins);
					defPlayerClip.showScore(_turn.solve.defenderScore, defenseWins);
					
					if (showActionAnimation) {
						attPlayerClip.unlockFromSolvePhase();
						defPlayerClip.unlockFromSolvePhase();
						setupActionAnimation();
					}
					_playArea.solveOverlay.addLogRow(msg);
					//_playArea.solveOverlay.solveText.htmlText = msg + _playArea.solveOverlay.solveText.htmlText;
					break;
				case LogRowType.SCORE:
					attPlayerClip = _playArea.fieldMc.playerList[_turn.solveScore.shooter.toString()];
					defPlayerClip = _playArea.fieldMc.playerList[_turn.solveScore.goalkeeper.toString()];
					
					msg = attPlayerClip.playerName + " has a chance to score!";
					_playArea.playMenu.setTargetingMessage(msg);
					_playArea.clearActionArrow();
					_playArea.showActionArrow('score', _playArea.fieldMc.playerList[attPlayerClip.instanceId], _playArea.fieldMc.playerList[defPlayerClip.instanceId]);
					
					attPlayerClip.lockUpForSolvePhase();
					defPlayerClip.lockUpForSolvePhase();
					
					if (_turn.solveScore.autoWinner == GameRole.IDLE) {
						attPlayerClip.rollDie(_turn.solveScore.attackerRoll);
						defPlayerClip.rollDie(_turn.solveScore.defenderRoll);
						
					}
					else {
						if (_turn.solveScore.autoWinner == GameRole.ATTACK) {
							
							//msg = attPlHtmlText + " scores!<br>";
						}
						else {
							
							//msg = defPlHtmlText + " saves!<br>";
						}
					}
					
					break;
				case LogRowType.END_SCORE:
					attPlayerClip = _playArea.fieldMc.playerList[_turn.solveScore.shooter.toString()];
					defPlayerClip = _playArea.fieldMc.playerList[_turn.solveScore.goalkeeper.toString()];
					
					attPlayerClip.hideDie();
					defPlayerClip.hideDie();
					
					attPlayerClip.lockUpForSolvePhase();
					defPlayerClip.lockUpForSolvePhase();
					attPlHtmlText = getPlayerNameHtml(attPlayerClip);
					defPlHtmlText = getPlayerNameHtml(defPlayerClip);
					if (_turn.solveScore.goalScored == 1) {
						msg = attPlHtmlText + " scores!<br>";
						attackWins = true;
						defenseWins = false;
						if (!_skip) {
							_playArea.showGoalAnimation(attPlayerClip);
						}
					}
					else {
						msg = defPlHtmlText + " saves!<br>";
						attackWins = false;
						defenseWins = true;
					}
					if (_turn.solveScore.autoWinner == GameRole.IDLE) {
						
						msg = defPlHtmlText + " rolled " + _turn.solveScore.defenderRoll + ", his score is " + _turn.solveScore.defenderScore + "<br>" + msg;
						msg = attPlHtmlText + " rolled " + _turn.solveScore.attackerRoll + ", his score is " + _turn.solveScore.attackerScore + "<br>" + msg;
						
						
					}
					else {
						if (_turn.solveScore.autoWinner == GameRole.ATTACK) {
							
							//msg = attPlHtmlText + " scores!<br>";
						}
						else {
							
							//msg = defPlHtmlText + " saves!<br>";
						}
					}
					
					attPlayerClip.showScore(_turn.solveScore.attackerScore, attackWins);
					defPlayerClip.showScore(_turn.solveScore.defenderScore, defenseWins);
					
					//_playArea.solveOverlay.solveText.htmlText = msg + _playArea.solveOverlay.solveText.htmlText;
					_playArea.solveOverlay.addLogRow(msg);
					break;
				case LogRowType.CAST_INIT:
					_currentPlayerId = logRow.sourcePlayer;
					_playArea.fieldMc.playerList[_currentPlayerId.toString()].lockUpForSolvePhase();
					msg = getPlayerNameHtml(_playArea.fieldMc.playerList[_currentPlayerId.toString()]) + " is trying to cast " + logRow.text + "<br>";
					_playArea.solveOverlay.addLogRow(msg);
					break;
				case LogRowType.CAST:
					showCast(logRow.cast);
					break;
				case LogRowType.CAST_END:
					break;
				case LogRowType.ABILITY:
					showPlayerProc(logRow.proc);
					break;
				case LogRowType.INTERRUPT:
					showCardProc(logRow.proc);
					break;
				case LogRowType.PROC:
					showPlayerProc(logRow.proc);
					break;
				case LogRowType.SWAP:
					//_playArea.fieldMc.setPlayerToPos(_playArea.fieldMc.playerList[logRow.sourcePlayer.toString()], logRow.targetPos.sign);
					if (_skip)
						_playArea.fieldMc.setPlayerToPos(_playArea.fieldMc.playerList[logRow.sourcePlayer.toString()], logRow.targetPos.sign);
					else {
						
						var tl:TimelineMax = _playArea.fieldMc.animatePlayerToPos(_playArea.fieldMc.playerList[logRow.sourcePlayer.toString()], logRow.targetPos.sign, true, false);
					}
					
					if (_playArea.ballHandler == logRow.sourcePlayer.toString())
						_playArea.updateBallHandler(logRow.targetId.toString());
					msg = "Swapping " + getPlayerNameHtml(_playArea.fieldMc.playerList[logRow.sourcePlayer.toString()], false);
					msg += " with " + getPlayerNameHtml(_playArea.fieldMc.playerList[logRow.targetId.toString()], false);
					_playArea.solveOverlay.addLogRow(msg);
					break;
				default:
					break;
			}
			_logCounter++;
			if (_logCounter == _turn.logRows.length)
				_finished = true;
			_timer.delay = logRow.type.delay;
			
		}
		
		private function clearCurrentLogItem():void {
			var msg:String;
			var attPlayerClip:PlayerClip;
			var defPlayerClip:PlayerClip;
			
			var logRow:GameLogRow = _turn.logRows[_logCounter - 1];
			switch (logRow.type) {
				case LogRowType.DEBUG:
					break;
				case LogRowType.INIT:
					break;
				case LogRowType.MAIN:
					attPlayerClip = _playArea.fieldMc.playerList[_turn.solve.attackerId.toString()];
					defPlayerClip = _playArea.fieldMc.playerList[_turn.solve.defenderId.toString()];
					
					attPlayerClip.unlockFromSolvePhase();
					defPlayerClip.unlockFromSolvePhase();
					
					break;
				case LogRowType.END_MAIN:
					attPlayerClip = _playArea.fieldMc.playerList[_turn.solve.attackerId.toString()];
					defPlayerClip = _playArea.fieldMc.playerList[_turn.solve.defenderId.toString()];
					
					attPlayerClip.hideScore();
					defPlayerClip.hideScore();
					
					attPlayerClip.unlockFromSolvePhase();
					defPlayerClip.unlockFromSolvePhase();
					break;
				case LogRowType.SCORE:
					attPlayerClip = _playArea.fieldMc.playerList[_turn.solveScore.shooter.toString()];
					defPlayerClip = _playArea.fieldMc.playerList[_turn.solveScore.goalkeeper.toString()];
					
					attPlayerClip.unlockFromSolvePhase();
					defPlayerClip.unlockFromSolvePhase();
					
					break;
				case LogRowType.END_SCORE:
					attPlayerClip = _playArea.fieldMc.playerList[_turn.solveScore.shooter.toString()];
					defPlayerClip = _playArea.fieldMc.playerList[_turn.solveScore.goalkeeper.toString()];
					
					attPlayerClip.hideScore();
					defPlayerClip.hideScore();
					
					attPlayerClip.unlockFromSolvePhase();
					defPlayerClip.unlockFromSolvePhase();
					break;
				case LogRowType.CAST_INIT:
					/*
					_currentPlayerId = logRow.sourcePlayer;
					_playArea.fieldMc.playerList[_currentPlayerId.toString()].unlockFromSolvePhase();
					*/
					break;
				case LogRowType.CAST_END:
					if (_currentPlayerId != 0) {
						_playArea.fieldMc.playerList[_currentPlayerId.toString()].unlockFromSolvePhase();
						var pl:PlayerClip = _playArea.fieldMc.playerList[_currentPlayerId.toString()];
						if (pl.side == 'home')
							_ac.moveToSide(pl.side, _homeAcList.length);
						else
							_ac.moveToSide(pl.side, _awayAcList.length);
					}
					
					/*
					_playArea.removeChild(_ac);
					_ac = null;
					*/
					break
				case LogRowType.ABILITY:
					if (_currentPlayerId != 0)
						_playArea.fieldMc.playerList[_currentPlayerId.toString()].unlockFromSolvePhase();
					break;
				case LogRowType.SWAP:
					_playArea.fieldMc.playerList[logRow.sourcePlayer.toString()].unlockFromSolvePhase();
					_playArea.fieldMc.playerList[logRow.targetId.toString()].unlockFromSolvePhase();
					break;
			}
			
		}
		
		private function setupActionAnimation():void {
			if (!_skip) {
				switch (_turn.solve.action) {
					case GameAction.DRIBBLE:
						var tl:TimelineMax = _playArea.fieldMc.animatePlayerToPos(_playArea.fieldMc.playerList[_turn.solve.attackerId.toString()], _turn.solve.target.sign, true, false);
						break;
					case GameAction.PASS:
						var targetId:String;
						if (_playArea.engine.game.playerTeam.formation.hasPlayerById(_turn.solve.attackerId))
							targetId = _playArea.engine.game.playerTeam.getPlayer(_turn.solve.target).instanceId.toString();
						else
							targetId = _playArea.engine.game.opponentTeam.getPlayer(_turn.solve.target).instanceId.toString();
						var targetPoint:Point = _playArea.getRelativePlayerPosition(_turn.solve.attackerId.toString(), targetId);
						
						_playArea.timeline = _playArea.ball.animatePassTo(targetPoint);
						_playArea.timeline.addCallback(_playArea.updateBallHandler, 2, new Array(targetId.toString()));
						break;
				}
				
			}
			
		}
		
		
		private function getRoleHtml(role:GameRole):String {
			var roleColor:String;
			if (role == _playArea.clientRole)
				roleColor = USER_PLAYER_COLOR;
			else
				roleColor = OPPONENT_PLAYER_COLOR;
			
			return "<b><font color='#" + roleColor + "'>" + role.name + "</font></b>";
		}
		
		private function getPlayerNameHtml(pl:PlayerClip, highlight:Boolean = true):String {
			var plColor:String;
			if (highlight)
				pl.lockUpForSolvePhase();
			if (pl.side == _playArea.clientSide)
				plColor = USER_PLAYER_COLOR;
			else
				plColor = OPPONENT_PLAYER_COLOR;
			
			return "<b><font color='#" + plColor + "'>" + pl.playerName + "</font></b>";
		}
		
		private function showCast(cast:GameCast):void {
			if (_currentPlayerId != 0)
				_playArea.fieldMc.playerList[_currentPlayerId.toString()].unlockFromSolvePhase();
			_currentPlayerId = cast.casterId;
			var pl:PlayerClip = _playArea.fieldMc.playerList[_currentPlayerId.toString()];
			var effectTarget:PlayerClip;
			var plHtmlText:String = getPlayerNameHtml(pl);
			_roundTexts[_currentSection] = '';
			cast.text = cast.text.replace("<", "&lt;");
			cast.text = cast.text.replace(">", "&gt;");
			
			var card:GameActionCard = new GameActionCard();
			card.copy(CardManager.getCardManager().createCardInstance(cast.cardId, 1));
			
			_ac = new ActionCardClip(card, _playArea, 1, "roundView");
			
			_ac.x = _playArea.acMiddle.x;
			_ac.y = _playArea.acMiddle.y;
			_playArea.addChild(_ac);
			if (pl.side == 'home')
				_homeAcList.push(_ac);
			else
				_awayAcList.push(_ac);
			
			if (cast.status == GameCastStatus.PLAYED) {
				_roundTexts[_currentSection] += plHtmlText + " casts " + cast.name + ": " + cast.text + "<br>";
				if (cast.cost != 0)
					pl.addAnimatedStatValue(0 - cast.cost, 'stamina');
				for (var p:uint; p < cast.effects.length; p++) {
					
					effectTarget = _playArea.fieldMc.playerList[cast.effects[p].target.toString()];
					//Logger.debug('UI > Round Viewer > ' + cast.name + ' effect #' + p.toString() + " with target " + effectTarget.playerName);
					for (var prop:String in cast.effects[p].stats) {
						if ((Stats.isBaseStatName(prop))&&(cast.effects[p].stats[prop] != 0))
							effectTarget.addAnimatedStatValue(cast.effects[p].stats[prop], prop);
					}
				}
				
			}
			else if (cast.status == GameCastStatus.COUNTERED) {
				if (cast.cost != 0)
					pl.addAnimatedStatValue(0 - cast.cost, 'stamina');
				_roundTexts[_currentSection] += plHtmlText + " tried to cast " + cast.name + ": " + cast.text + " but it was countered!<br>";
			}
			else if (cast.status == GameCastStatus.MAX_CAST_REACHED) {
				_roundTexts[_currentSection] += plHtmlText + " tried to cast " + cast.name + ": " + cast.text + " but he couldn't play any more cards!<br>";
			}
			else if (cast.status == GameCastStatus.CONDITION) {
				_roundTexts[_currentSection] += plHtmlText + " tried to cast " + cast.name + ": " + cast.text + " but he didn't meet some condition!<br>";
			}
			//_playArea.solveOverlay.solveText.htmlText = _roundTexts[_currentSection] + _playArea.solveOverlay.solveText.htmlText;
			_playArea.solveOverlay.addLogRow(_roundTexts[_currentSection]);
		}
		
		
		private function showPlayerProc(proc:GameProc):void {
			if (_currentPlayerId != 0)
				_playArea.fieldMc.playerList[_currentPlayerId.toString()].unlockFromSolvePhase();
			_currentPlayerId = proc.sourcePlayer
			var pl:PlayerClip = _playArea.fieldMc.playerList[_currentPlayerId.toString()];
			var effectTarget:PlayerClip;
			var plHtmlText:String = getPlayerNameHtml(pl);
			var proc:GameProc;
			var msg:String = '';
			proc.label = proc.label.replace("<", "&lt;");
			proc.label = proc.label.replace(">", "&gt;");
			
			msg += plHtmlText + " triggered '" + proc.label + "'<br>";
			
			for (var p:uint; p < proc.effects.length; p++) {
				
				effectTarget = _playArea.fieldMc.playerList[proc.effects[p].target.toString()];
				//Logger.debug('UI > Round Viewer > ' + proc.label + ' effect #' + p.toString() + " with target " + effectTarget.playerName);
				for (var prop:String in proc.effects[p].stats) {
					if ((Stats.isBaseStatName(prop))&&(proc.effects[p].stats[prop] != 0))
						effectTarget.addAnimatedStatValue(proc.effects[p].stats[prop], prop);
				}
			}
			_playArea.solveOverlay.addLogRow(msg);
			
		}
		
		private function showCardProc(proc:GameProc):void {
			
			if (_currentPlayerId != 0)
				_playArea.fieldMc.playerList[_currentPlayerId.toString()].unlockFromSolvePhase();
			var effectTarget:PlayerClip;
			
			var msg:String = '';
			proc.label = proc.label.replace("<", "&lt;");
			proc.label = proc.label.replace(">", "&gt;");
			
			msg += proc.label + " triggered<br>";
			//Logger.debug('UI > Round Viewer > ' + proc.label + ' has ' + proc.effects.length + ' effects');
			for (var p:uint; p < proc.effects.length; p++) {
				
				effectTarget = _playArea.fieldMc.playerList[proc.effects[p].target.toString()];
				Logger.debug('UI > Round Viewer > ' + proc.label + ' effect #' + p.toString() + " with target " + effectTarget.playerName);
				for (var prop:String in proc.effects[p].stats) {
					if ((Stats.isBaseStatName(prop))&&(proc.effects[p].stats[prop] != 0))
						effectTarget.addAnimatedStatValue(proc.effects[p].stats[prop], prop);
				}
			}
			_playArea.solveOverlay.addLogRow(msg);
		}
		
		private function getActionMessage(action:GameAction, target:Position):String {
			var msg:String;
			msg = _playArea.fieldMc.playerList[_playArea.ballHandler].playerName + " is ";
			switch (action) {
				case GameAction.DRIBBLE:
					msg += "dribbling to " + _playArea.engine.game.attackingTeam.getPlayer(target).name + "'s position [" + target.sign + "]";
					break;
				case GameAction.PASS:
					msg += "passing to " + _playArea.engine.game.attackingTeam.getPlayer(target).name + " [" + target.sign + "]";
					break;
				case GameAction.CROSS:
					msg += "crossing to " + _playArea.engine.game.attackingTeam.getPlayer(target).name + " [" + target.sign + "]";
					break;
				case GameAction.SHOOT:
					msg += "dribbling to shoot";
					break;
			}
			return msg;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function start():void {
			Logger.debug('UI > Round Viewer > Starting');
			_running = true;
			
			_playArea.solveOverlay.addLogRow("<b>" + _playArea.scoreboard.roundCounterBox.text + " Started</b><br><br>");
			//_playArea.logBox.dbgTextBox.text = "Round Viewer Started";
			_timer = new Timer(_delay);
			_timer.addEventListener(TimerEvent.TIMER, timeUpHandler, false, 0, true);
			showNextLogItem();
			_timer.start();
		}
		public function stopView():void {
			if (_running) {
				_running = false;
				Logger.debug('UI > Round Viewer > Stopping');
				
				_playArea.solveOverlay.addLogRow("<b>" +  _playArea.scoreboard.roundCounterBox.text + " Complete!</b><br>");
				if (_timer.running)
					_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER, timeUpHandler);
				_playArea.playerSkipsLog = true;
				_playArea.playButton.textLabel.text = 'Next Turn';
			}
			
		}
		
		public function clearActionCards():void {
			var i:uint;
			for (i = 0; i < _homeAcList.length; i++) {
				_ac = _homeAcList[i];
				_playArea.removeChild(_ac);
			}
			_homeAcList.length = 0;
			for (i = 0; i < _awayAcList.length; i++) {
				_ac = _awayAcList[i];
				_playArea.removeChild(_ac);
			}
			_awayAcList.length = 0;
		}
		
		public function skipToEnd():void {
			_skip = true;
			if (_timer.running)
				_timer.stop();
			timeUpHandler(new TimerEvent(TimerEvent.TIMER));
		}
		
	}
}